package com.itheima.manager;

import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.DistributionVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Liguoqiang
 * @date 2021/10/13 9:09
 */
@Component
public class UserManager {
    /*
     * 实战后台
     * 用户管理模块
     * 查询用户列表
     * */

    @Reference
    private UserInfoService userInfoService;

    public PageBeanVo findUserList(Integer pageNum, Integer pageSize) {
            PageBeanVo pageBeanVo = userInfoService.findUsersByPage(pageNum, pageSize);
            return pageBeanVo;
    }

    /*
     * 实战后台
     * 用户管理模块
     * 查询用户详情
     * */
    public UserInfo findUserMessages(Long userId) {
        return userInfoService.findById(userId);
    }

    //抽取封装方法
    public ResponseEntity define(Map<String,Integer> map){
        //2.声明voList
        List<DistributionVo> voList = new ArrayList<>();
        //3.遍历map
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            //4.封装Vo
            DistributionVo vo = new DistributionVo();
            vo.setName(key);
            vo.setValue(map.get(key));
            //5.添加到voList
            voList.add(vo);
        }
        //6.返回
        return ResponseEntity.ok(voList);
    }

    //城市分布表
    public ResponseEntity selectByCity() {
        Map<String, Integer> map = userInfoService.selectByCity();
        return define(map);
    }
    //年龄分布表
    public ResponseEntity selectByAge() {
        Map<String,Integer> map = userInfoService.selectByAge();
        return define(map);
    }
    //性别分布
    public ResponseEntity selectByGender() {
        Map<String, Integer> map = userInfoService.selectByGender();
        return define(map);
    }
    //行业排名
    public ResponseEntity selectByProfession() {
        Map<String, Integer> map = userInfoService.selectByProfession();
        return define(map);
    }
}
